import './profile.less';
import Header from "../../../components/Header";
import {Link} from "react-router";
import {connect} from "react-redux";
import NoData from "../../../components/nodata";
import Search from "../../../components/search";
import Panel from "../../../elements/Panel";
import Pagination from "../../../components/Pagination/";
import Config from "../../../libs/config";
import {SUBJECT, PAGE_SUBJECT} from "../../../actions/subject";
import {fetch, fetch_remove, enable_delete_btn, selectAllItem, selectItem,setDefaultUser} from "../../../actions";
import {md5Url, URL} from '../../../libs/Url';
import {MD5, Fetch} from '../../../libs/';
import FormItemRef from '../../../components/FormItemRef';
import FormItemType from '../../../components/FormItemType';

const Feedback = React.createClass({
    getInitialState(){
        function onchange(index, content) {
            this.setState({
                form: [
                    ...self.state.form.slice(0, index),
                    Object.assign({}, self.state.form[index], {
                        value: content,
                        tips: content.length ? '' : '该项必填',
                    }),
                    ...self.state.form.slice(index + 1),
                ]
            })
        }

        return {
            form: [
                {
                    name: 'name',
                    label: '原始密码',
                    value: '',
                    required: true,
                    type: FormItemType.PASSWORD,
                    onchange: onchange,
                },
                {
                    name: 'gender',
                    label: '新密码',
                    value: '',
                    value_: '',
                    required: true,
                    type: FormItemType.PASSWORD,
                    onchange: onchange,
                },
                {
                    name: 'title',
                    label: '确认密码',
                    value: '',
                    required: true,
                    type: FormItemType.PASSWORD,
                    onchange: onchange,
                },
            ]
        }
    },
    componentDidMount(){
        console.log('mount')
    },

    render(){
        const self = this;
        const {dispatch, user} = this.props;
        const data = self.props || {};
        var form = this.state.form;
        return (
            <Panel>
                <Header>修改密码</Header>
                <div className="table-list-head row profile">
                    <FormItemRef
                        form={form}
                        addRow={true}
                        labelAlign="right"
                        textWidth={8}
                        onsubmit={(data)=> {
                            if (!data.length) {
                                return alert('请填写信息');
                            }
                            var c = {};
                            if (data[1].value != data[2].value) {
                                return alert('确认密码不正确');
                            }
                            Fetch(URL.USER_UPDATE_PWD, {
                                body: {
                                    oldPassword: MD5(data[0].value),
                                    newPassword: MD5(data[1].value)
                                }
                            })
                                .then(res=> {
                                    console.log(res);
                                    if(res.retCode){
                                        {/*alert('修改成功!')*/}
                                    }else{
                                        alert('修改成功!')
                                        Fetch(URL.Get_LOGOUT)
                                            .then(res=> {
                                                dispatch(setDefaultUser({}));
                                            })
                                            .catch(e=> {
                                                console.log(e)
                                            })
                                    }

                                })
                                .catch(e=> {
                                    console.log(e);
                                });

                        }}
                    />
                </div>
            </Panel>
        )
    }
});
function select(state) {
    return {
        user: state.user.user,
    }
}
export default connect(select)(Feedback)